/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.bool;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.user.client.ui.*;

import finbudgettable.client.ui.grid.Utils;
import finbudgettable.client.ui.grid.column.FilterPanel;
import finbudgettable.client.ui.grid.filter.Filter;
import finbudgettable.client.ui.grid.filter.FilterChangeEvent;
import finbudgettable.client.ui.grid.filter.FilterChangeHandler;

public class BoolFilterPanel extends VerticalPanel implements FilterPanel {

    interface Strings extends Messages {
        @DefaultMessage("Фильтр")
        String filter();

        @DefaultMessage("Без фильтра")
        String withoutFilter();

        @DefaultMessage("Выбранные")
        String selected();

        @DefaultMessage("Невыбранные")
        String notSelected();

        @DefaultMessage("Установить")
        String apply();
    }

    static final Strings STRINGS = GWT.create(Strings.class);
    private final String fieldName;
    private final ListBox listBox;

    public BoolFilterPanel(String fieldName) {
        super();
        this.fieldName = fieldName;
        Grid grid = new Grid(2, 2);
        grid.setStyleName(BoolCell.RESOURCES.style().bBoolFilterPanel());
        Label label = new Label(STRINGS.filter());
        listBox = new ListBox();
        listBox.addItem(STRINGS.withoutFilter());
        listBox.addItem(STRINGS.selected());
        listBox.addItem(STRINGS.notSelected());
        Button applyButton = new Button(STRINGS.apply());
        applyButton.setStyleName(Utils.blueButton);
        applyButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                fireEvent(new FilterChangeEvent(createFilter()));
            }
        });
        grid.setWidget(0, 0, label);
        grid.setWidget(0, 1, listBox);
        grid.setWidget(1, 1, applyButton);
        add(grid);
    }

    private Filter createFilter() {
        int index = listBox.getSelectedIndex();
        if (index == 0) {
            return null;
        }
        if (index == 1) {
            return new TrueBoolFilter(fieldName);
        }
        return new FalseBoolFilter(fieldName);
    }

    @Override
    public void setFilter(Filter filter) {
        if (filter != null) {
            if (filter.getClass().equals(TrueBoolFilter.class)) {
                listBox.setSelectedIndex(1);
                return;
            } else if (filter.getClass().equals(FalseBoolFilter.class)) {
                listBox.setSelectedIndex(2);
                return;
            }
        }
        listBox.setSelectedIndex(0);
    }

    @Override
    public void addFilterChangeHandler(FilterChangeHandler handler) {
        addHandler(handler, FilterChangeEvent.TYPE);
    }
}
